﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Crosland.Offline.Logic;
using Crosland.Offline.Core;

namespace Crosland.Offline.WinForm
{
    public partial class Materiales : Form
    {
        public Materiales()
        {
            InitializeComponent();
        }

        private void Materiales_Load(object sender, EventArgs e)
        {
            dgwListaMaterial.AutoGenerateColumns = false;
            //CargarCombo();
        }

        //private void CargarCombo()
        //{
        //    AlmacenLogic al = new AlmacenLogic();

        //    List<Almacen> lstAlmacen = new List<Almacen>();
        //    lstAlmacen = al.GetByCriteria("1301");
        //    cmbAlmacen.DisplayMember = "DesAlmacen";
        //    cmbAlmacen.ValueMember = "IdAlmacen";
        //    cmbAlmacen.DataSource = lstAlmacen;
        //}

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            MaterialLogic ma = new MaterialLogic();

            List<Material> lstMaterial = new List<Material>();
            lstMaterial = ma.GetByCriteria(txtMATNR.Text, txtMAKTX.Text, "0002", txtMarca.Text, txtModelo.Text);
            dgwListaMaterial.DataSource = lstMaterial;
            dgwListaMaterial.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        }

        private void dgwListaMaterial_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                List<Material> lstMat = new List<Material>();

                Material objMat = new Material();
                lstMat = (List<Material>)dgwListaMaterial.DataSource;
                objMat = lstMat[e.RowIndex];
                lstMatAñadido.Items.Add(objMat.Matnr.Trim() + " - " + objMat.Maktx.Trim());
            }
        }

        private void txtMATNR_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        private void lstMatAñadido_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int index = this.lstMatAñadido.IndexFromPoint(e.Location);
            lstMatAñadido.Items.RemoveAt(index);
        }

        private void btnLimpiar_Click(object sender, EventArgs e)
        {
            lstMatAñadido.Items.Clear();
        }            
    }
}